package com.itheima.day20.demo06;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 5. 转换流一次读取一个字符.						//必须掌握.
 * 6. 转换流一次读取一个字符数组.
 * <p>
 * 7. 普通的字符流一次读写一个字符.
 * 8. 普通的字符流一次读写一个字符数组.
 * <p>
 * 9. 高效的字符流一次读写一个字符.
 * 10. 高效的字符流一次读写一个字符数组.
 * 11. 高效的字符流一次读写一行.
 */
public class demo02 {
    public static final String SRC = "./homework/src/com/itheima/day20/demo06/aa.txt";
    public static final String DEST = "./homework/src/com/itheima/day20/demo06/dd.txt";

    public static void main(String[] args) throws Exception {
        //5. 转换流一次读取一个字符.
        //method01();

        //6. 转换流一次读取一个字符数组.
        //method02();

        //7. 普通的字符流一次读写一个字符.
        //method07();

        //普通的字符流一次读写一个字符数组.
        //method8();

        //高效的字符流一次读写一个字符.
        //method9();

        //高效的字符流一次读写一个字符数组.
        //method10();

        //高效的字符流一次读写一行.
        //method11();


    }

    public static void method11() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File(SRC)));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(DEST)));

        String line;
        while ((line = br.readLine()) != null) {
            bw.write(line);
            bw.newLine();
        }

        bw.close();
        br.close();
    }

    public static void method10() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File(SRC)));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(DEST)));
        int cha;
        char[] chars = new char[1024];
        while ((cha = br.read(chars)) != -1) {
            bw.write(chars, 0, cha);
        }

        bw.close();
        br.close();
    }

    public static void method9() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(new File(SRC)));
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(DEST)));
        int cha;
        while ((cha = br.read()) != -1) {
            bw.write(cha);
        }

        bw.close();
        br.close();
    }

    public static void method8() throws IOException {
        FileReader fr = new FileReader(new File(SRC));
        FileWriter fw = new FileWriter(new File(DEST));
        int c = 0;
        char[] chars = new char[1024];
        while ((c = fr.read(chars)) != -1) {
            fw.write(chars, 0, c);
        }
        fw.close();
        fr.close();
    }

    public static void method07() throws IOException {
        FileReader fr = new FileReader(new File(SRC));
        FileWriter fw = new FileWriter(new File(DEST));
        int c;
        while ((c = fr.read()) != -1) {
            fw.write(c);
        }
        fw.close();
        fr.close();
    }

    public static void method02() throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(SRC)), StandardCharsets.UTF_8);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File(DEST)), StandardCharsets.UTF_8);
        char[] chars = new char[1024];
        int len = 0;
        while ((len = isr.read(chars)) != -1) {
            osw.write(chars, 0, len);
        }
        osw.close();
        isr.close();
    }

    public static void method01() throws IOException {
        InputStreamReader isr = new InputStreamReader(new FileInputStream(new File(SRC)), StandardCharsets.UTF_8);
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File(DEST)), StandardCharsets.UTF_8);

        int len = 0;
        while ((len = isr.read()) != -1) {
            osw.write(len);
        }
        osw.close();
        isr.close();
    }
}
